/**
 * 方法一：比对路由权限
 * 方法二：指定返回的默认路由
 */

/**
 * 
 * @param {Array} userRouter 后台返回的路由权限json
 * @param {Array} allRouter 前端配置好的路由权限数据
 * @returns{Array} realRoutes 过滤之后的符合条件的路由
 */
export function recursionRouter(userRouter = [], allRouter = []) {
  var realRoutes = []
  allRouter.forEach((v, i) => {
    userRouter.forEach((item, index) => {
      if (item.name === v.meta.name) {
        if (item.children && item.children?.length > 0) {
          v.children = recursionRouter(item.children, v.children)
        }
        realRoutes.push(v)
      }
    })
  })
  return realRoutes
}
export function setDefaultRoute(routes) {
  routes.forEach((v, i) => {
    if (v.children && v.children.length > 0) {
      v.redirect = { name: v.children[0].name }
      setDefaultRoute(v.children)
    }
  })
}

export function generateRouter(userRouter = [], baseUrl = '') {
  const handlePath = (path) => {
    return path.replace(/\/{2,}/g, '/')
  }
  let newRouter = userRouter.map(r => {
    let router = {
      path: r.route,
      name: r.route,
      meta: {
        name: r.name,
        icon: r.icon
      },
      component: () => import(handlePath(`@/views/${baseUrl}/${r.route}.vue`))
    }
    console.log(handlePath(`@/views/${baseUrl}/${r.route}.vue`), '跳转的路径');
    if (r.children) {
      router.children = generateRouter(r.children, r.route)
    }
    return router
  })
  return newRouter
}



